class UrlManget(object):

    def __init__(self):
        self.new_url = set()    # 未爬取的url
        self.old_url = set()    # 已爬取的url

    def has_new_url(self):
        '''
        判断是否有新的url
        :return:
        '''
        return self.new_url_size()!=0

    def get_new_url(self):
        '''
        获取未爬取的url
        :return:
        '''
        new_url = self.new_url.pop()    #把未爬取的url拿出来一个
        self.old_url.add(new_url)   #放到已爬取的url中
        return new_url

    def add_new_url(self, url):
        '''
        把新的url保存到未爬取url中
        :return:
        '''
        if url is None:
            return
        if url not in self.old_url and url not in self.new_url:
            self.new_url.add(url)

    def add_new_urls(self, urls):
        '''
        把新的url保存到未爬取url中
        :param urls: url的集合
        :return:
        '''
        if urls is None or len(urls)==0:
            return
        for url in urls:
            self.new_url.add(url)

    def new_url_size(self):
        '''
        返回未爬取url的长度
        :return:
        '''
        return len(self.new_url)

    def old_url_size(self):
        '''
        返回已爬取的url的长度
        :return:
        '''
        return len(self.old_url)